﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Configuration;

using System.Net;
using System.Net.Mail;
using System.Net.Configuration;

/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{
    public const string USER_LOGIN_SESSION = "UserLogin";
    public const string ADMIN_LOGIN_SESSION = "AdminLogin";

    protected DTO.User UserLogin
    {
        get
        {
            return (DTO.User)Session[USER_LOGIN_SESSION];
        }
        set
        {
            Session[USER_LOGIN_SESSION] = value;
        }
    }

    protected DTO.Admin AdminLogin
    {
        get
        {
            return ( DTO.Admin ) Session[ADMIN_LOGIN_SESSION];
        }
        set
        {
            Session[ADMIN_LOGIN_SESSION] = value;
        }
    }

    public BasePage ()
    {
    }

    protected void SendEmail (string fromName, string toName, string toEmail, string subject, string body)
    {
        System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
        MailSettingsSectionGroup settings = ( MailSettingsSectionGroup ) config.GetSectionGroup("system.net/mailSettings");

        var fromAddress = new MailAddress(settings.Smtp.Network.UserName, fromName);
        var toAddress = new MailAddress(toEmail, toName);

        var smtp = new SmtpClient
        {
            Host = settings.Smtp.Network.Host,
            Port = settings.Smtp.Network.Port,
            EnableSsl = settings.Smtp.Network.EnableSsl,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password)
        };

        using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body })
        {
            smtp.Send(message);
        }
    }

    protected DateTime ConvertStringToDate ( string value )
    {
        DateTime date = DateTime.MinValue;
        DateTime.TryParseExact(value, System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date);
        return date;
    }
    protected string ConvertDateToString ( DateTime date )
    {
        return date.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);
    }

}